939. The square of the sum

 

Find the square of the sum of the digits of a two-digit positive integer.

 

Input. One two-digit positive integer n.

 

Output. Print the square of the sum of the digits of the number n.

 

Sample input

Sample output

23

25

 

 

SOLUTION

mathematics

 

Algorithm analysis

Let n =  be a two-digit input number. Find its digits of tens a and ones b:

·        a = n / 10;

·        b = n % 10.

Then the square of the sum of digits of number n will be the value of (a + b)2.

 

Algorithm realization

Read the value of n. Find the number of tens a and the number of ones b.

 

scanf("%d",&n);  

a = n / 10;

b = n % 10;

 

Compute and print the answer.

 

res = (a + b) * (a + b);

printf("%d\n",res);  

 

Algorithm realization with formatted input

 

#include <stdio.h>

 

int a, b, res;

 

int main(void)

{

  scanf("%1d%1d",&a,&b);  

  res = (a + b) * (a + b);

  printf("%d\n",res);  

  return 0;

}

 

Algorithm realization string

 

#include <stdio.h>

 

char s[100];

int a, b, res;

 

int main(void)

{

  gets(s); 

  a = s[0] - '0';

  b = s[1] - '0';

  res = (a + b) * (a + b);

  printf("%d\n",res);

  return 0;

}

 

Algorithm realization – STL

 

#include <iostream>

using namespace std;

 

int n, a, b, res;

 

int main(void)

{

  cin >> n;  

  a = n / 10; b = n % 10;

  res = (a + b) * (a + b);

  cout << res << endl;  

  return 0;

}

 

Java realization

 

import java.util.*;

 

public class Main

{

  public static void main(String []args)

  {

    Scanner con = new Scanner(System.in);

    int n = con.nextInt();  

    int a = n / 10, b = n % 10;

    int res = (a + b) * (a + b);

    System.out.println(res);

  }

}   

 

Java realization – read a line

 

import java.util.*;

 

public class Main

{

  public static void main(String []args)

  {

    Scanner con = new Scanner(System.in);

    String s = con.nextLine();  

    int a = s.charAt(0) - '0', b = s.charAt(1) - '0';

    int res = (a + b) * (a + b);

    System.out.println(res);

  }

}   

 

Python realization

 

n = int(input())

a = n // 10

b = n % 10

print((a + b) ** 2)